package net.comm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.comm.user.db.DbUsersService;
import net.comm.user.db.UsersServiceSqlMysql;
import net.comm.util.DbcpPool;
import net.comm.util.StringUtil;

import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;

 
public class VerityInfo extends BaseServlet {
 
	static Logger logger = Logger.getLogger(VerityInfo.class.getName());
	
    /**
	 * 
	 */
	private static final long serialVersionUID = -556767186511743106L;

	@Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException,
           IOException {
 	   
      	//待
   		String id = getValue(request, "id");
   		//
   		String value = getValue(request, "value");
		
		value = StringUtil.chiDeUTF8(value);

   		
        setNoCache(request, response);
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");  
           
        PrintWriter out = response.getWriter();
		
        StringBuffer sb = new StringBuffer();
        Connection cn = null;
		try
		{

			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];

			fields[0] = value;
			
			UsersServiceSqlMysql servSql = new UsersServiceSqlMysql();

			BasicSqlService  bSqlServ = new BasicSqlService();
			
			 String sql = "";
			 if(id.equals("email")){										//Email

				sql = servSql.getByEmailSql();

				List list = bSqlServ.dbRead(cn, sql, fields, new DbUsersService());
				
				if(list.size() > 0){
					sb.append("该Email，已经注册！");
				}else{
					
					sb.append("该Email还没有注册，欢迎加入！");
				}
			 }else if(id.equals("nick")){									//昵称

				sql = servSql.getCountByNickSql();

				int cnt = bSqlServ.dbCount(cn, sql, fields);

				if(cnt > 0){
					
					sb.append("该昵称在此刻已经有人使用！");
				}else{
					
					sb.append("该昵称在此刻还没有人使用！");
				}
			 }   		
			
			DbcpPool.closeTran(cn);			
			DbcpPool.returnConnection(cn);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
		} finally {
			DbcpPool.returnConnection(cn);
		}

        out.print(sb.toString());
          
        out.flush();
   }
 
}

